package com.gui.reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ReflectionClassApi {

    public static void main(String[] args) {
        Class<ReflectionEntity> eClass = ReflectionEntity.class;
        try {
            ReflectionEntity entity = eClass.getDeclaredConstructor(Double.class, Double.class).newInstance(1.83d, 78d);
            System.out.println(entity);

            Method[] methods = eClass.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println(method.getName());
            }

            Method setAttribute = eClass.getDeclaredMethod("setAttribute", Map.class);
            setAttribute.setAccessible(true);
            Map<String, Object> attributeMap = new HashMap<>();
            attributeMap.put("addr", "陕西省汉中市城固县");
            Object result = setAttribute.invoke(entity, attributeMap);
            System.out.println(result);
            System.out.println(entity);

            System.out.println("============================");

            Field field = eClass.getDeclaredField("attribute");
            field.setAccessible(true);
            attributeMap.put("nickname", "男神");
            field.set(entity, attributeMap);
            System.out.println(entity);

            System.out.println("=============================");
            Deprecated annotation = eClass.getAnnotation(Deprecated.class);
            System.out.println(annotation.since());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
